#include "player.h"
#include "ui_player.h"

Player::Player(QTcpSocket *s, QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Player)
{
    ui->setupUi(this);
    socket = s;

    play_flag = STOP_PLAY;

    //建立信号与槽
    connect(socket,&QTcpSocket::readyRead,this,&Player::server_reply_slot);

    //启动定时器，6秒后发送获取所有音乐的请求
    timer.start(10000);
    connect(&timer,&QTimer::timeout,this,&Player::timeout_slot);

}

Player::~Player()
{
    delete ui;
}

void Player::closeEvent(QCloseEvent *event)
{
    QJsonObject obj;
    obj.insert("cmd","app_offline");

    QByteArray ba = QJsonDocument(obj).toJson();
    socket->write(ba);

    socket->waitForBytesWritten();
    //关掉窗口
    event->accept();
}

void Player::timeout_slot()
{
    //向服务器发送获取所有音乐的请求
    QJsonObject obj;
    obj.insert("cmd","app_music");

    QByteArray ba = QJsonDocument(obj).toJson();
    socket->write(ba);
    socket->waitForBytesWritten();

    timer.stop();
}

void Player::server_reply_slot()
{
    QByteArray ba = socket->readAll();
    QJsonObject obj = QJsonDocument::fromJson(ba).object();
    QString cmd = obj.value("cmd").toString();

    if(cmd == "app_reply")              //播放器操作的回复
    {
        QString result = obj.value("result").toString();
        if(result == "start_success")   //开始播放音乐成功
        {
            ui->startButton->setText("||");
            play_flag = START_PLAY;
        }
        else if(result == "suspend_success")    //暂停播放音乐成功
        {
            ui->startButton->setText("|>");
            play_flag = SUSPEND_PLAY;
        }
        else if(result == "continue_success")   //继续播放音乐成功
        {
            ui->startButton->setText("||");
            play_flag = START_PLAY;
        }
        else if(result == "off_line")
        {
            QMessageBox::warning(this,"操作提示","音箱下线");
        }

    }
    else if(cmd == "app_reply_status")  //播放器状态的回复
    {
        //回复的信息包含三种：（1）播放状态（2）当前歌曲名（3）音量
        QString status = obj.value("status").toString();
        if(status == "start")           //如果播放器是正在播放的状态
        {
                play_flag=START_PLAY;
                ui->startButton->setText("||");
        }
        else if(status == "suspend")    //如果播放器是暂停播放的状态
        {
                play_flag=SUSPEND_PLAY;
                ui->startButton->setText("|>");
        }
        else if(status == "stop")
        {
                play_flag=STOP_PLAY;
                ui->startButton->setText("|>");
        }

        QString music_name = obj.value("music").toString();
        ui->curMusicName->setText(music_name);

        int voice = obj.value("voice").toInt();
        ui->voiceLabel->setText(QString::number(voice));

    }
    else if(cmd == "app_reply_music")   //获取所有音乐的回复
    {
        QJsonArray arr = obj.value("music").toArray();      //转换成json数组
        QString result;
        for(int i=0;i<arr.count();i++)
        {
            result.append(arr.at(i).toString());
            result.append('\n');
        }

        ui->musicEdit->setText(result);
    }
}


void Player::on_startButton_clicked()
{
    QJsonObject obj;
    if(play_flag == SUSPEND_PLAY)
    {
        obj.insert("cmd","app_continue");
    }
    else if(play_flag == START_PLAY)
    {
        obj.insert("cmd","app_suspend");
    }else if(play_flag == STOP_PLAY)
    {
        obj.insert("cmd","app_start");
    }

    QByteArray ba = QJsonDocument(obj).toJson();

    socket->write(ba);
    socket->waitForBytesWritten();
}

void Player::on_priorButton_clicked()
{
    QJsonObject obj;
    obj.insert("cmd","app_prior");
    QByteArray ba = QJsonDocument(obj).toJson();

    socket->write(ba);
    socket->waitForBytesWritten();
}

void Player::on_nextButton_clicked()
{
    QJsonObject obj;
    obj.insert("cmd","app_next");
    QByteArray ba = QJsonDocument(obj).toJson();

    socket->write(ba);
    socket->waitForBytesWritten();
}

void Player::on_voiceDownButton_clicked()
{
    QJsonObject obj;
    obj.insert("cmd","app_voice_down");
    QByteArray ba = QJsonDocument(obj).toJson();

    socket->write(ba);
    socket->waitForBytesWritten();
}

void Player::on_voiceUpButton_clicked()
{
    QJsonObject obj;
    obj.insert("cmd","app_voice_up");
    QByteArray ba = QJsonDocument(obj).toJson();

    socket->write(ba);
    socket->waitForBytesWritten();
}

//顺序播放
void Player::on_seqButton_clicked()
{
    QJsonObject obj;
    obj.insert("cmd","app_sequence");
    QByteArray ba = QJsonDocument(obj).toJson();

    socket->write(ba);
    socket->waitForBytesWritten();
}

//随机播放
void Player::on_randomButton_clicked()
{
    QJsonObject obj;
    obj.insert("cmd","app_random");
    QByteArray ba = QJsonDocument(obj).toJson();

    socket->write(ba);
    socket->waitForBytesWritten();
}

//单曲循环
void Player::on_circleButton_clicked()
{
    QJsonObject obj;
    obj.insert("cmd","app_circle");
    QByteArray ba = QJsonDocument(obj).toJson();

    socket->write(ba);
    socket->waitForBytesWritten();
}
